package com.lin.entity;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Component
public class SecurityUser implements UserDetails {

    private Integer uid;
    private String username;
    private String password;
    private String strRole;
    private List<GrantedAuthority> authorities;

    public SecurityUser(User user) {
        uid = user.getUid();
        username = user.getUsername();
        password = user.getPassword();
        strRole = user.getRole();
        authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(user.getRole());
    }

    @Override
    public List<GrantedAuthority> getAuthorities() {
        return authorities;
    }


    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public Integer getUid() {
        return uid;
    }

    public String getStrRole() {
        return strRole;
    }

}
